package cc.w3d.jawos.apps.artiks.client.components;

import java.util.List;

import cc.w3d.apps.cats.catsManager.client.beans.Cat;
import cc.w3d.gwitjeds.maskedtextbox.client.DateTimeBox;
import cc.w3d.gwitjeds.richtexteditor.client.RichTextEditor;
import cc.w3d.jawos.apps.artiks.client.Artiks;
import cc.w3d.jawos.apps.artiks.client.rpc.ArtiksAsyncCallback;
import cc.w3d.jawos.apps.artiks.client.widgets.IconToolBarButton;
import cc.w3d.jawos.apps.artiks.client.widgets.ToolBar;
import cc.w3d.jawos.apps.artiks.manager.client.beans.Article;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RequiresResize;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ArticleEditor extends Composite implements RequiresResize
{
	private ResourceTreeItem selectedTreeItem;

	private TextBox        titol    = new TextBox();
	private TextBox        subtitol = new TextBox();
	private RichTextEditor body     = new RichTextEditor();
	private DateTimeBox    dateBox  = new DateTimeBox();
	private TextBox        img      = new TextBox();
	private TextBox        intro    = new TextBox();
	private ListBox        cats     = new ListBox();

	VerticalPanel          main = new VerticalPanel();

	public ArticleEditor()
	{
		ToolBar toolBar = new ToolBar();
		IconToolBarButton saveButton = new IconToolBarButton(Artiks.icons.save(), new ClickHandler()
		{
			public void onClick(ClickEvent event)
			{
				Artiks.service.setArticle(selectedTreeItem.getId(), getArticleToSave(), 
						new ArtiksAsyncCallback<Void>() {@Override public void onFinish(Void result) {}});
			}
		});

		saveButton.setTitle("Desar els canvis a l'article actual");

		toolBar.addChild(saveButton);

		main.add(toolBar);
//		DateTimeFormat dateFormat = DateTimeFormat.getLongDateFormat();
//	    dateBox.setFormat(new DateBox.DefaultFormat(dateFormat));
	    HorizontalPanel dateRow = new HorizontalPanel();
	    dateRow.add(new HTML("<i><b>Data</b></i>: &nbsp;"));
	    dateRow.add(dateBox);
	    main.add(dateRow);
	    HorizontalPanel catsRow = new HorizontalPanel();
	    catsRow.add(new HTML("<i><b>Categoria</b></i>: &nbsp;"));
	    catsRow.add(cats);
	    cats.setVisibleItemCount(1);
	    main.add(catsRow);
	    main.add(new HTML("<i><b>Títol</b></i>"));
		titol.setWidth("100%");
		main.add(titol);
		main.add(new HTML("<br><i><b>Subtítol</b></i>"));
		subtitol.setWidth("100%");
		main.add(subtitol);
		main.add(new HTML("<br><i><b>Icona:</b></i>"));
		img.setWidth("100%");
		main.add(img);
		main.add(new HTML("<br><i><b>Introducció</b></i>"));
		intro.setWidth("100%");
		main.add(intro);
		main.add(new HTML("<br><i><b>Contingut de l'article</b></i>"));
		main.add(body);

		initWidget(main);

		setWidth("100%");
		setHeight("100%");
		main.setWidth("100%");
		main.setHeight("100%");
		body.setWidth("100%");
		body.setHeight("100%");
	}

	Article getArticleToSave()
	{
		Article article = new Article();

		article.titol    = titol.getText();
		article.subtitol = subtitol.getText();
		article.body     = body.getText();
		article.date     = dateBox.getValue();
		article.img      = img.getValue();
		article.intro    = intro.getValue();
		article.cat      = new Integer(cats.getValue(cats.getSelectedIndex()));

		return article;
	}

	public void loadCats()
	{
		Artiks.service.getCats(new ArtiksAsyncCallback<List<Cat>>()
				{
					@Override
					public void onFinish(List<Cat> result)
					{
						for (Cat cat : result)
							cats.addItem(cat.name, cat.id.toString());
					}
				});
	}

	public void setSelectedTreeItem(ResourceTreeItem selectedTreeItem)
		{this.selectedTreeItem = selectedTreeItem;}

	private int getCatIndexByVal(Integer cat)
	{
		if(cat == null) return 0;

		for(int i = 0; i < cats.getItemCount(); i++)
			if(new Integer(cats.getValue(i)).equals(cat))
				return i;

		return 0;
	}

	@Override
	protected void onLoad()
	{
		super.onLoad();
		Artiks.service.getArticle(selectedTreeItem.getId(), new ArtiksAsyncCallback<Article>() {
			
			@Override
			public void onFinish(Article result)
			{
				titol.setText    (result.titol);
				subtitol.setText (result.subtitol);
				body.setText     (result.body);
				dateBox.setValue (result.date);
				img.setValue     (result.img);
				intro.setValue   (result.intro);
				cats.setSelectedIndex(getCatIndexByVal(result.cat));
			}
		});
		main.setCellHeight(body, "100%");
	}

	public void onResize()
	{
		body.onResize();
	}
}